package com.example.whale.utils;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

import androidx.annotation.NonNull;

import com.iflytek.cloud.SpeechUtility;

import com.example.whale.MainActivity;
import com.example.whale.utils.xfyun.ResultCallback;
import com.example.whale.utils.xfyun.XFApiUtils;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
 * @Description: java类作用描述
 * @Author: 下雨了
 * @CreateDate: 2022-03-31 16:14
 * @UpdateUser: 更新者
 * @UpdateDate: 2022-03-31 16:14
 * @UpdateRemark: 1、添加 科大讯飞 语音识别 接口
 *                2、xfVoiceStart 开始录音
 *                3、xfVoiceStop 结束录音
 *                4、xfVoiceCancel 取消录音
 *                5、xfVoiceGetResult 获取录音结果
 *                6、xfVoiceSetLanguage 语言设置
 *                7、xfVoiceSetPunctuation 设置是否有标点符号
 *                8、xfVoiceSetVoiceBOS 设置语音前端点
 *                9、xfVoiceSetVoiceEOS 设置语音后端点
 * @Version: 1.0
 */
public class MethodChannelUtils {
    public static MethodChannel methodChannel;
    public static final String SYS_MIUI = "sys_miui";
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

    public void init(Context context, BinaryMessenger messenger) {
        methodChannel = new MethodChannel(messenger, "com.topnet.xxx");

        methodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {

                switch (call.method) {

                    case "xfVoiceStart": // 开始录音

                        XFApiUtils.getInstance(context).start(new ResultCallback() {
                            @Override
                            public void callback(String str) {
                                methodChannel.invokeMethod("xfVoiceSetResult", str);
                                // result.success(str);
                            }
                        });
                        break;
                    case "xfVoiceStop": // 结束录音
                        XFApiUtils.getInstance(context).stop();
                        result.success(true);
                        break;
                    case "xfVoiceCancel": // 取消录音
                        XFApiUtils.getInstance(context).cancel();
                        result.success(true);
                        break;
                    case "xfVoiceGetResult": // 获取语音结果
                        String str = XFApiUtils.getInstance(context).getResult();
                        result.success(str);
                        break;
                    case "xfVoiceSetLanguage": // 语言设置
                        String language = call.argument("language");
                        if (language == null || language.length() == 0) {
                            return;
                        }
                        XFApiUtils.getInstance(context).setLanguage(language);
                        result.success(true);
                        break;

                    case "xfVoiceSetPunctuation": // 标点符号 （默认有）,设置为"0"返回结果无标点,设置为"1"返回结果有标点
                        String punctuation = call.argument("punctuation");
                        if (punctuation != "0" || punctuation != "1") {
                            return;
                        }
                        XFApiUtils.getInstance(context).setPunctuation(punctuation);
                        result.success(true);
                        break;
                    case "xfVoiceSetVoiceBOS": // 设置语音前端点:静音超时时间(0-10000)，即用户多长时间不说话则当做超时处理
                        String bos = call.argument("bos");
                        XFApiUtils.getInstance(context).setVoiceBOS(bos);
                        result.success(true);
                        break;
                    case "xfVoiceSetVoiceEOS": // 设置语音后端点:后端点静音检测时间(0-10000)，即用户停止说话多长时间内即认为不再输入， 自动停止录音
                        String eos = call.argument("eos");
                        XFApiUtils.getInstance(context).setVoiceEOS(eos);
                        result.success(true);
                        break;

                    case "isXiaomi":
                        try {
                            Properties prop = new Properties();

                            prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));

                            if (prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null

                                    || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null

                                    || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null) {

                                // SYS = SYS_MIUI;//小米
                                result.success(true);

                            } else {
                                result.success(false);

                            }
                        } catch (Exception e) {
                            result.success(false);
                        }

                        break;
                    case "openApp":

                        Intent intent1 = new Intent(context, MainActivity.class);
                        context.startActivity(intent1);

                        break;
                }
            }
        });
    }
}
